using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PicAFlick.App
{
    public partial class FolderListControl : UserControl
    {
        public FolderListControl()
        {
            InitializeComponent();
        }


        public delegate void SelectedFolderChanged(PicAFlick.Domain.Folder folder);


        private PicAFlick.Domain.Folder _rootFolder;
        public event SelectedFolderChanged SelectionChange;
        public PicAFlick.Domain.Folder RootFolder
        {
            get
            {
                return _rootFolder;
            }
            set
            {
                _rootFolder = value;

                if (_rootFolder != null)
                {
                    IList<PicAFlick.Domain.Folder> folders = new List<PicAFlick.Domain.Folder>();
                    folders.Add(_rootFolder);
                    this.flowLayoutPanel1.Controls.Clear();
                    PopulateFolderList(0, folders);
                }
                
            }

        }

        private void PopulateFolderList(int level, IList<PicAFlick.Domain.Folder> folders)
        {
            foreach (PicAFlick.Domain.Folder f in folders)
            {
                FolderItem fi = new FolderItem(f, level);
                
                this.flowLayoutPanel1.Controls.Add(fi);
                //fi.Anchor = AnchorStyles.Top;
                fi.Click += new EventHandler(ClickItem);
                fi.Width = this.Width;

                
                PopulateFolderList(level + 1, f.SubFolders);
            }
        }

        void ClickItem(object sender, EventArgs e)
        {
            PicAFlick.Domain.Folder f = (PicAFlick.Domain.Folder)((FolderItem)sender).Folder;
            if (SelectionChange != null)
            {

                SelectionChange(f);
            }

        }


    }
}
